package com.example.pcl.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.pcl.constant.MessageConstants;
import com.example.pcl.exception.ObjectException;
import com.example.pcl.mapper.CounselorsMapper;
import com.example.pcl.mapper.StudentsMapper;
import com.example.pcl.pojo.dto.StudentFeedbackDTO;
import com.example.pcl.pojo.dto.StudentFeedbackFilterDTO;
import com.example.pcl.pojo.entity.StudentFeedback;
import com.example.pcl.mapper.StudentFeedbackMapper;
import com.example.pcl.pojo.vo.StudentFeedbackVO;
import com.example.pcl.result.PageResult;
import com.example.pcl.service.IStudentFeedbackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 学生评价表，记录学生对心理咨询师的评价 服务实现类
 * </p>
 *
 * @author xx
 * @since 2024-12-17
 */
@Service
@RequiredArgsConstructor
public class StudentFeedbackServiceImpl extends ServiceImpl<StudentFeedbackMapper, StudentFeedback> implements IStudentFeedbackService {


    private final StudentsMapper studentMapper;


    private final CounselorsMapper counselorMapper;

    @Override
    public PageResult<StudentFeedbackVO> listFeedbacks(@NotNull StudentFeedbackFilterDTO filterDTO) {

        // 构造分页查询
        Page<StudentFeedback> page = new Page<>(filterDTO.getPageNum(), filterDTO.getPageSize());
        LambdaQueryWrapper<StudentFeedback> queryWrapper = new LambdaQueryWrapper<StudentFeedback>()
                .eq(filterDTO.getStudentId() != null, StudentFeedback::getStudentId, filterDTO.getStudentId())
                .eq(filterDTO.getCounselorId() != null, StudentFeedback::getCounselorId, filterDTO.getCounselorId())
                .eq(filterDTO.getRating() != null, StudentFeedback::getRating, filterDTO.getRating());

        // 执行分页查询
        Page<StudentFeedback> resultPage = baseMapper.selectPage(page, queryWrapper);

        // 转换为 VO
        List<StudentFeedbackVO> voList = resultPage.getRecords().stream().map(feedback -> {
            StudentFeedbackVO vo = new StudentFeedbackVO();
            BeanUtils.copyProperties(feedback, vo);
            vo.setStudentName(feedback.getStudentId() == null ? "" : studentMapper.selectById(feedback.getStudentId()).getName());
            vo.setCounselorName(counselorMapper.selectById(feedback.getCounselorId()).getName());
            return vo;
        }).toList();

        return new PageResult<>(resultPage.getTotal(), voList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addFeedback(StudentFeedbackDTO feedbackDTO) {
        StudentFeedback feedback = new StudentFeedback();
        BeanUtils.copyProperties(feedbackDTO, feedback);
        int rows = baseMapper.insert(feedback);
        if (rows <= 0) {
            throw new ObjectException(MessageConstants.ADD_ERR);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateFeedback(StudentFeedbackDTO feedbackDTO) {
        StudentFeedback feedback = new StudentFeedback();
        BeanUtils.copyProperties(feedbackDTO, feedback);
        int rows = baseMapper.updateById(feedback);
        if (rows <= 0) {
            throw new ObjectException(MessageConstants.UPDATE_ERR);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteFeedback(List<Integer> feedbackId) {
        if (CollectionUtil.isEmpty(feedbackId)) {
            throw new ObjectException(MessageConstants.DELETE_LIST_NULL);
        }

        if (!removeBatchByIds(feedbackId.stream().distinct().toList())) {
            throw new ObjectException(MessageConstants.DELETE_ERR);
        }
    }
}
